package com.cqjtu.education.service.impl;

import com.cqjtu.education.entity.CourseData;
import com.cqjtu.education.dao.CourseDataDao;
import com.cqjtu.education.service.CourseDataService;
import com.cqjtu.education.utils.AliyunOSS;
import com.cqjtu.education.utils.Result;
import com.github.pagehelper.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;


import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * (CourseData)表服务实现类
 *
 * @author makejava
 * @since 2022-04-20 15:47:53
 */
@Service("courseDataService")
public class CourseDataServiceImpl implements CourseDataService {
    @Resource
    private CourseDataDao courseDataDao;

    @Resource
    private AliyunOSS aliyunOSS;

    @Override
    public List<CourseData> queryByParentPath(String parentPath) {
        return this.courseDataDao.queryByParentPath(parentPath);
    }

    /**
     * 通过ID查询单条数据
     *
     * @param dataId 主键
     * @return 实例对象
     */
    @Override
    public CourseData queryById(String dataId) {
        return this.courseDataDao.queryById(dataId);
    }


    /**
     * 新增数据
     *
     * @param courseData 实例对象
     * @return 实例对象
     */
    @Override
    public CourseData insert(CourseData courseData, MultipartFile multipartFile) {
        this.aliyunOSS.upload(courseData, multipartFile);
        return courseData;
    }


    @Override
    public CourseData rename(String dataId, String objectName, String newName) {
        if (this.aliyunOSS.scan(objectName, newName)) {
            //重复名称，请换一个名字进行修改
            return null;
        }
        CourseData courseData = this.courseDataDao.queryById(dataId);
        this.aliyunOSS.reNameFile(courseData.getParentPath(), objectName, newName);
        courseData.setFileName(newName);
        courseData.setFileSuffix(newName.substring(newName.lastIndexOf(".") + 1, newName.length()));
        System.out.println(courseData.toString());
        this.courseDataDao.update(courseData);
        return courseData;
    }

    @Override
    public int newFolder(CourseData courseData) {
        this.aliyunOSS.newFolder(courseData.getParentPath() + courseData.getFileName());
        courseData.setDataId(UUID.randomUUID().toString());
        courseData.setCreateTime(new Date());
        courseData.setFile(false);
        courseData.setFileSize("0B");
        courseData.setFileSuffix("");
        String url = "https://cqjtu-education.oss-cn-beijing.aliyuncs.com/" + courseData.getParentPath() + courseData.getFileName();
        courseData.setDataUrl(url);
        return this.courseDataDao.insert(courseData);
    }

    /**
     * 通过主键删除数据
     *
     * @param dataId 主键
     * @return 是否成功
     */
    @Override
    public int delete(String dataId, String objectName) {
        this.aliyunOSS.delete(objectName);
        return this.courseDataDao.deleteById(dataId);
    }

    @Override
    public int deleteByBatch(List<String> dataIdList, List<String> objectNameList) {
        this.aliyunOSS.batchDelete(objectNameList);
        int count = 0;
        for (String id : dataIdList) {
            this.courseDataDao.deleteById(id);
            count++;
        }
        return count;
    }

    @Override
    public int deleteFolder(String dataId, String objectName) {
        this.aliyunOSS.deleteFolder(objectName);
        this.courseDataDao.deleteById(dataId);
        return this.courseDataDao.deleteFolder(objectName);
    }
}
